using System;
using System.Collections.Generic;
using System.Text;

namespace Barbar.MapReport.PolygonReducer {
  internal class Graph {
    private SortedList<decimal, SortedList<decimal, Node>> m_Points = new SortedList<decimal, SortedList<decimal, Node>>();

    public IEnumerable<Node> GetNodesWithAtLeast3AdjectNodes() {
      foreach (SortedList<decimal, Node> list in m_Points.Values)
        foreach (Node node in list.Values)
          if (node.AdjectNodes.Count > 2)
            yield return node;
    }

    //public IEnumerable<Node> GetNodesWithAtMost2AdjectNodes() {
    //  foreach (SortedList<decimal, Node> list in m_Points.Values)
    //    foreach (Node node in list.Values)
    //      if (node.AdjectNodes.Count <= 2)
    //        yield return node;
    //}


    public Node this[Node point] {
      get {
        if (m_Points.ContainsKey(point.X) && m_Points[point.X].ContainsKey(point.Y))
          return m_Points[point.X][point.Y];
        else
          return null;
      }
    }

    public void RemoveRange(IEnumerable<Node> range) {
      foreach (Node node in range)
        RemovePoint(node);
    }

    public void RemovePoint(Node point) {
      if (m_Points.ContainsKey(point.X)) {
        if (m_Points[point.X].ContainsKey(point.Y))
          m_Points[point.X].Remove(point.Y);
        if (m_Points[point.X].Count == 0)
          m_Points.Remove(point.X);
      }
    }

    public void AddPoint(Node point) {
      if (!m_Points.ContainsKey(point.X))
        m_Points.Add(point.X, new SortedList<decimal, Node>());
      if (!m_Points[point.X].ContainsKey(point.Y))
        m_Points[point.X].Add(point.Y, point);
    }
  }
}
